import shutil
import os
import sys
import time

def findfolers(keywords,from_dir,target_dir):
    result_search=[]#查找结果
    for dirName, subdirList, fileList in os.walk(from_dir):#筛选所在目录所有列表
        for i in subdirList:#寻找包含关键词名称的二级路径
            if keywords in i:
                result_search.append([dirName,i])
    if result_search:
        for i in result_search:
            a=os.path.join(i[0],i[1])
            b=os.path.join(target_dir,i[1])
            c=os.path.join(target_dir,'临时拷贝文件夹')
            shutil.copytree(a,c)
            shutil.move(c,b)
            # shutil.rmtree(c)
        print('查找成功:'+keywords)
    else:
        print('找不到：'+keywords)

def main():
    with open('路径设置.txt') as f:
        dirs=f.readlines()
    rootDir=dirs[1].strip('\n')
    target_dir=os.path.join(dirs[4].strip('\n'),'查找结果'+time.strftime('%Y-%m-%d-%H-%M-%S'))
    with open('查找对象（一行一个关键词）.txt') as f2:
        for keyword_search in f2.readlines():       #查找关键词
            findfolers(keywords=keyword_search.strip('\n'),from_dir=rootDir,target_dir=target_dir)
    input('检索结束')
if __name__ == '__main__':
    main()